package com.bang.concurrency.example.threadPool;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

/**
 * @ClassName: ThreadPoolExample1
 * @Desc: ExecutorService executorService = Executors.newFixedThreadPool(3);
 * @auther: BANG
 * @Date: 2019/10/09 14:15
 * @Version: 1.0
 * @Modified By:
 */
@Slf4j
public class ThreadPoolExample2 {

    public static void main(String[] args) throws Exception {
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        log.info("创建线程池");
        Thread.sleep(10000);
        for (int i = 0; i < 10; i++) {
            int index = i;
            executorService.execute(() -> {
                log.info("running {}", index);
            });
        }
        executorService.shutdown();
        log.info("关闭线程池");

    }

    /**
     *
     *  public static ExecutorService newFixedThreadPool(int nThreads) {
     *         return new ThreadPoolExecutor(nThreads, nThreads,
     *                                       0L, TimeUnit.MILLISECONDS,
     *                                       new LinkedBlockingQueue<Runnable>());
     *     }
     *
     *
     */
    public static ExecutorService newFixedThreadPool(int nThreads) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>());
    }

}
